﻿using Client.Constant;
using Client.Framework.Event;
using Client.Framework.Message;
using Client.Framework.Socket;
using Client.Message.Request;
using Client.Message.Response;
using Client.Model;
using Client.View;
using Client.View.PermissionView;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Client.ViewModel.PermissionViewModel
{
    /// <summary>
    /// Author: Leil
    /// Description: PermissionEditViewModel
    /// CreateTime: 2020/5/28 19:13:25
    /// </summary>
    public class PermissionEditViewModel : BindableBase
    {
        public PermissionEdit Page { get; private set; }

        public PermissionEditViewModel(PermissionEdit page)
        {
            Page = page;

            SaveCommand = new DelegateCommand(SaveInfo);

            MsgEventSubscribe();
        }

        private void MsgEventSubscribe()
        {
            MessageEventPublisher.SubscribeEvent(MessageId.OPT_RESP, ReceiveOptMsg);
        }

        /// <summary>
        /// 初始化
        /// </summary>
        public void Init(ViewPermisson permisson, PermissionListViewModel parentDataContext)
        {
            ParentDataContext = parentDataContext;
            Title = permisson == null ? "新增权限" : "编辑权限";
            if (permisson == null)
            {
                ViewPermisson = new ViewPermisson();
                return;
            }
            ViewPermisson = permisson;
        }

        private ViewPermisson permisson;

        public ViewPermisson ViewPermisson
        {
            get { return permisson; }
            set
            {
                permisson = value;
                RaisePropertyChanged("ViewPermisson");
            }
        }


        /// <summary>
        /// 标题
        /// </summary>
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                RaisePropertyChanged("Title");
            }
        }


        /// <summary>
        /// 父级页面的DataContext
        /// </summary>
        private PermissionListViewModel ParentDataContext { get; set; }

        /// <summary>
        /// 保存角色信息
        /// </summary>
        public DelegateCommand SaveCommand { get; private set; }
        public void SaveInfo()
        {
            AddEditPermissionReq msg = new AddEditPermissionReq(MessageId.ADD_EDIT_PERMISSION)
            {
                Permisson = ViewPermisson
            };
            ApplicationClient.Instance().Send(msg);
        }

        /// <summary>
        /// 操作回复消息接收
        /// </summary>
        /// <param name="message">回复消息</param>
        private void ReceiveOptMsg(ResponseBaseMessage message)
        {
            if (message is OperationResp msg)
            {
                System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    ConfirmTip tip = new ConfirmTip()
                    {
                        Message = { Text = msg.Content }
                    };
                    DialogHost.Show(tip, "PermissionEditPage", CloseTip);
                }));
            }
        }


        public void CloseTip(object sender, DialogClosingEventArgs eventArgs)
        {
            Page.Hide();
            if (ParentDataContext != null)
            {
                ParentDataContext.GetPermissions();
            }
        }


    }
}
